@*
    For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@
@{
    Layout = "_Layout";
}
<body class="layui-anim layui-anim-up">
    <div class="x-nav">
        <span class="layui-breadcrumb">
            <a href="/Admin/Admin/Index">首页</a>
            <a>
                <cite>作品</cite>
            </a>
        </span>
        <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" href="javascript:location.replace(location.href);" title="刷新">
            <i class="layui-icon" style="line-height:30px">ဂ</i>
        </a>
    </div>
    <div class="x-body">
        <xblock>
            <button class="layui-btn j-add"><i class="layui-icon"></i>添加</button>
            <div class="layui-inline">
                <input type="text" placeholder="请输入作品名" autocomplete="off" class="layui-input search_input">

            </div>
            <button class="layui-btn search_btn" lay-filter="sreach"><i class="layui-icon">&#xe615;</i></button>
        </xblock>
        <table class="layui-table" lay-data="{ url:'/Admin/Works/GetWork', page:true, id:'work', where:{content:''}}" lay-filter="work">
            <thead>
                <tr>
                    <th lay-data="{field:'', width:'5%', sort: true,type:'numbers'}">ID</th>
                    <th lay-data="{field:'Title', width:'10%'}">标题</th>
                    <th lay-data="{field:'Sort', width:'8%',edit: 'text'}">序号</th>

                    <th lay-data="{field:'State', width:'8%',templet: '#barIsEnabled'}">状态</th>
                    <th lay-data="{field:'Describe', width:'20%'}">描述</th>
                    <th lay-data="{field:'TypeStr', width:'7%'}">类型</th>
                    <th lay-data="{field:'Price', width:'7%'}">价格</th>
                    <th lay-data="{field:'ClassName', width:'7%'}">分类</th>
                    <!-- <th lay-data="{field:'IsRecommend', width:'7%',templet: '#barIsXianShi'}">是否推荐</th> -->
                    <th lay-data="{field:'CoverImg', width:'20%', templet: '#imgUrl'}">封面图片</th>
                    <th lay-data="{field:'AddDT' , width:'10%',templet: '<div>{{ HsCms.ToDate(d.AddDT) }}</div>'}">添加时间</th>
                    <th lay-data="{field:'' , width:'15%' , templet: '#barDemo'}">操作</th>
                </tr>
            </thead>
        </table>
        <script type="text/html" id="imgUrl">
            <a href="{{d.CoverImg}}" target="_blank">{{d.CoverImg}}</a>
        </script>
        <script type="text/html" id="barIsEnabled">
            {{#  if(d.State){ }}
            <input type="checkbox" checked="" lay-skin="switch" lay-text="启用|禁用" lay-filter="ChkIsEnableds" aid="{{ d.Id }}">
            {{#  }else{ }}
            <input type="checkbox" lay-skin="switch" lay-text="启用|禁用" lay-filter="ChkIsEnableds" aid="{{ d.Id }}">
            {{#  } }}
        </script>
        <script type="text/html" id="barIsXianShi">
            {{#  if(d.IsRecommend){ }}
            <input type="checkbox" checked="" lay-skin="switch" lay-text="启用|禁用" lay-filter="ChkIsRecommend" aid="{{ d.Id }}">
            {{#  }else{ }}
            <input type="checkbox" lay-skin="switch" lay-text="启用|禁用" lay-filter="ChkIsRecommend" aid="{{ d.Id }}">
            {{#  } }}
        </script>
        <script type="text/html" id="barDemo">
           
            <a class="layui-btn layui-btn-xs" lay-event="guanli">管理</a>
            
            <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
        </script>
    </div>
    <script>
layui.use(['laydate', 'table', 'form','layer'], function () {
            var laydate = layui.laydate;
            var table = layui.table;
            var $ = layui.jquery;
            var form = layui.form;
            var layer=layui.layer;


    //监听单元格编辑
  table.on('edit(work)', function(obj){
    var value = obj.value //得到修改后的值
    ,data = obj.data //得到所在行所有键值
    ,field = obj.field; //得到字段

if(value-0<=0){
	HsCms.Msg("请输入大于0的数字");
	return false;
}

    HsCms.AjaxPost("/Admin/Works/EditSort", "Id=" + data.Id + "&sort=" + value, function (result) {
                        if (result.code === 1) {
                            table.reload("work");
                        }
                        else {
                        }
                        HsCms.Msg(result.msg);
                    });
  });

            //启用、禁用
            form.on('switch(ChkIsEnableds)', function (data) {
                var sw = this;
                var vn = sw.checked ? "启用" : "禁用";
                var vis = sw.checked ? true : false;
                var vcid = $(sw).attr("aid");
                var x = sw.checked;
                HsCms.Confirm("您确定" + vn + "该数据吗？", function () {
                    HsCms.AjaxPost("/Admin/Works/IsEnabled", "Id=" + vcid + "&isEnabled=" + vis, function (result) {
                        if (result.code === 1) {
                            table.reload("work");
                        }
                        else {
                            sw.checked = !x;
                            form.render("checkbox");
                        }
                        HsCms.Msg(result.msg);
                    });
                }, function () {
                    sw.checked = !x;
                    form.render("checkbox");
                }, function () {
                    sw.checked = !x;
                    form.render("checkbox");
                });
            });

			form.on('switch(ChkIsRecommend)', function (data) {
				var sw = this;
				var vn = sw.checked ? "启用" : "禁用";
				var vis = sw.checked ? true : false;
				var vcid = $(sw).attr("aid");
				var x = sw.checked;
				HsCms.Confirm("您确定" + vn + "该数据吗？", function () {
					HsCms.AjaxPost("/Admin/Works/IsXianShi", "Id=" + vcid + "&isrecommend=" + vis, function (result) {
						if (result.code === 1) {
							table.reload("work");
						}
						else {
							sw.checked = !x;
							form.render("checkbox");
						}
						HsCms.Msg(result.msg);
					});
				}, function () {
					sw.checked = !x;
					form.render("checkbox");
				}, function () {
					sw.checked = !x;
					form.render("checkbox");
				});
			});


            //监听行工具事件
            table.on('tool(work)', function (obj) {
                var data = obj.data;
                //console.log(obj)
                if (obj.event === 'del') {
                    layer.confirm('您确定删除该作品吗，删除该作品会删除该作品下的所有章节内容。', function (index) {

                        $.ajax({
                            type: "post",
                            url: "/Admin/Works/Del",
                            data: { id: data.Id },
                            success: function (result) {
                                if (result.state == "ok") {
                                    layer.msg(result.msg)
                                } else {
                                    layer.msg(result.msg)
                                }
                            }
                        })

                        obj.del();

                    });
                } else if (obj.event === 'guanli') {
					var data = obj.data;
                    location.href="/Admin/Chapter/Index?workId="+data.Id;
                }
            });

            $(".j-add").click(function () {
                //var langstr = langs.join(',');
                HsCms.OpenPart(2, 'bann', '/Admin/Works/Add?type=add', "新增", ['900px', '700px'], ['新增', '取消'], function (layero) {

                }, function (index, layero) {
                    $(".btnEdit", layero.find("iframe")[0].contentWindow.document).click();
                });
            });



	var active = {
		reload: function () {
			//执行重载
			table.reload('work', {
				page: { curr: 1 }
				, where: { content: $.trim($(".search_input").val()) }
			});
		}
	};
	$('.search_input').bind('keyup', function (event) {
		if (event.keyCode == "13") {
			var type = 'reload';//data-type="reload"
			active[type] ? active[type].call(this) : '';
		}
	});
	//查找
	$(".search_btn").click(function () {
		var type = 'reload';
		active[type] ? active[type].call(this) : '';
	});


        });</script>
</body>